import java.awt.*;

import java.awt.event.*;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

import javax.swing.*;

public class TanChiShe {
	public static void main(String[] args) {
		JFrame f = new MyFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
	}
}

class MyFrame extends JFrame {
	private JButton kaishi = new JButton("开始");

	private JPanel btnpanel = new JPanel();

	private ShePanel panel = new ShePanel();

	private Thread th = null;

	private int state = unrun;

	private static final int run = 1;

	private static final int unrun = 2;

	public MyFrame() {
		this.init();
		this.addComponent();
		this.addListener();
	}

	private void start() {
		if (th == null) {
			th = new Thread(new Runnable() {
				public void run() {
					MyFrame.this.run();
				}
			});
			th.start();
			state = run;
		}
	}

	private void run() {
		try {
			while (true) {
				panel.setFocusable(true);
				panel.move();
				Thread.sleep(50);
			}
		} catch (Exception e) {
		}
	}

	private void interrupt() {
		th.interrupt();
		th = null;
		state = unrun;
	}

	private void addListener() {
		kaishi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (state == unrun) {
					start();
				} else if (state == run) {
					interrupt();
				}
			}
		});
	}

	private void addComponent() {
		this.add(panel);
		this.add(btnpanel, BorderLayout.SOUTH);
		this.btnpanel.add(kaishi);
		btnpanel.setBackground(Color.BLUE);
	}

	private void init() {
		this.setTitle("贪吃蛇");
		this.setSize(800, 600);
		this.setLocation(200, 100);

	}
}

class ShePanel extends JPanel {
	private ArrayList<Rectangle2D> rects = new ArrayList<Rectangle2D>();

	private Point p = new Point(740, 0);

	private int size = 10;

	private int len = 5;

	private boolean left = true;

	private boolean right = false;

	private boolean up = false;

	private boolean down = false;

	public ShePanel() {
		this.setFocusable(true);
		this.addComponent();
		this.addListener();
		this.initRects();
	}

	private void initRects() {
		Point point = new Point((int) p.getX(), (int) p.getY());
		for (int i = 0; i < len; i++) {
			Rectangle2D rect = new Rectangle2D.Double(point.getX(), point.getY(), size, size);
			rects.add(rect);
			point = new Point((int) (point.getX() + size), (int) point.getY());
		}

	}

	private void addComponent() {

	}

	private void addListener() {
		this.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				int code = e.getKeyCode();
				switch (code) {
				case KeyEvent.VK_LEFT:
					left = true;
					right = false;
					up = false;
					down = false;
					break;
				case KeyEvent.VK_RIGHT:
					left = false;
					right = true;
					up = false;
					down = false;
					break;
				case KeyEvent.VK_UP:
					left = false;
					right = false;
					up = true;
					down = false;
					break;
				case KeyEvent.VK_DOWN:
					left = false;
					right = false;
					up = false;
					down = true;
					break;
				default:
					return;
				}
			}
		});
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;

		for (int i = 0; i < rects.size(); i++) {
			g2.draw(rects.get(i));
		}
	}

	public void move() {
		Point p1 = null;
		if (left) {
			p1 = new Point((int) (p.getX() - size), (int) p.getY());
			if (p.getX() < 0) {
				JOptionPane.showMessageDialog(ShePanel.this, "Game Over！", "游戏结束", JOptionPane.INFORMATION_MESSAGE);
				System.exit(0);
			}
		}
		if (right) {
			p1 = new Point((int) (p.getX() + size), (int) p.getY());
			if (p.getX() > 800) {
				JOptionPane.showMessageDialog(ShePanel.this, "Game Over！", "游戏结束", JOptionPane.INFORMATION_MESSAGE);
				System.exit(0);
			}
		}
		if (up) {
			p1 = new Point((int) p.getX(), (int) (p.getY() - size));
			if (p.getY() < 0) {
				JOptionPane.showMessageDialog(ShePanel.this, "Game Over！", "游戏结束", JOptionPane.INFORMATION_MESSAGE);
				System.exit(0);
			}
		}
		if (down) {
			p1 = new Point((int) p.getX(), (int) (p.getY() + size));
			if (p.getY() > 600) {
				JOptionPane.showMessageDialog(ShePanel.this, "Game Over！", "游戏结束", JOptionPane.INFORMATION_MESSAGE);
				System.exit(0);
			}
		}
		p = p1;
		Rectangle2D rect = new Rectangle2D.Double(p.getX(), p.getY(), size, size);
		rects.add(0, rect);
		rects.remove(rects.size() - 1);
		repaint();
	}
}
